package link.bosswang.easyexcel;

import com.alibaba.excel.EasyExcel;
import link.bosswang.easyexcel.dto.EasyExcelItemVo;
import link.bosswang.easyexcel.strategy.ItemExportStrategy;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName: EasyExcelMain
 * @Description: '摘要'
 * @Author: 'Wei.Wang'
 * @Date: 2021/8/8 8:23 下午
 **/
public class EasyExcelMain {
    public static void main(String[] args) {
        ItemExportStrategy exportStrategy = new ItemExportStrategy();

        String fileName = "/Users/wang/WorkSpace/WangSpace/SwissArmyKnife/Tools/tools-easyexcel/target/ExportTest-" + System.currentTimeMillis() + ".xlsx";

        byte[] imgByUrl = getNetImgByUrl("https://img2.woyaogexing.com/2021/04/04/3e3be48247174838885b1adf28524c6b!60X60.jpeg");
        List<EasyExcelItemVo> dataList = createSingle("001", "99.00");
        dataList.get(0).setImage(imgByUrl);

        List<EasyExcelItemVo> twoItems = createSingle("002", "99.98");
        twoItems.get(0).setImage(imgByUrl);
        dataList.addAll(twoItems);

        HashSet<String> excludeFieldNames = new HashSet<String>();
        excludeFieldNames.add("itemPrice");

        EasyExcel.write(fileName, EasyExcelItemVo.class).excludeColumnFiledNames(excludeFieldNames)
                .registerWriteHandler(exportStrategy)
                .sheet()
                .doWrite(dataList);

    }

    public static List<EasyExcelItemVo> createSingle(String itemIndex, String price) {

        ArrayList<EasyExcelItemVo> list = new ArrayList<EasyExcelItemVo>();

        for (int index = 0; index < 8; index++) {
            EasyExcelItemVo itemVo = new EasyExcelItemVo();
            itemVo.setItemId("itemId" + itemIndex);
            itemVo.setItemTitle("商品标题" + itemIndex);
            itemVo.setItemOuterId("ItemOuterId" + itemIndex);
            itemVo.setItemPrice(new BigDecimal(price));

            itemVo.setSkuId("skuId" + itemIndex + "-" + index);
            itemVo.setSkuOuterId("skuOuterId" + itemIndex + "-" + index);
            itemVo.setSkuPro(itemIndex + "Sku规格" + "-" + index);
            itemVo.setSkuPrice(new BigDecimal(price));

            list.add(itemVo);
        }
        return list;
    }


    public static byte[] getNetImgByUrl(String strUrl) {
        try {
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(2 * 100000);
            // 通过输入流获取图片数据
            InputStream inStream = conn.getInputStream();
            // 得到图片的二进制数据
            byte[] btImg = readInputStream(inStream);
            return btImg;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        // 设置每次读取缓存区大小
        byte[] buffer = new byte[1024 * 10];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}
